#!/usr/bin/env python

# __  __            _   ____        _ _ ____        _   
#|  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_ 
#| |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|
#| |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_ 
#|_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|

# Framework: Circuits
# File: botadmin.py                                                            

# Primary contributor: Justin Giorgi (rebel_kid)

# Loved, not licensed

"A core plugin to send wallops and raw commands."

# Imports
from circuits.core import listener, Event, Component

# Creates event classes
class IRCmessage(Event): pass
class RAW(Event): pass

class Plugin(Component):

	def __init__(self, channel, cursor):
		"""Call the super constructors to initialize the Component.
		Then sets the channel and cursor. Finally sql statements
		are prepared."""
		super(Plugin, self).__init__()
		self.channel = channel
		self.cursor = cursor
		self.cursor("PREPARE allchans () AS SELECT channame, netid FROM channels WHERE active 1;")
		self.cursor("PREPARE chan (int) AS SELECT channame, netid FROM channels WHERE netid = $1 active = 1;")
		
	@listener("command-wallop")
	def wallop(self, **kwargs):
		"""If botadmin permissions allow and a message was provided the message is sent
		to all known channels. If conditions are not met a denied message is provided."""
		# Checks for botadmin permissions
		if kwargs["botadmin"] >= 2:
			
			# Checks for proper arguments
			if len(kwargs["command"]) > 1:
		
				# Sends wallop
				self.cursor.execute("EXECUTE allchans();")
				data = self.cursor.fetchall()
				for row in data:
					self.push(IRCmessage(row[0], " ".join(kwargs["command"][1:])), "4010", str(row[1]))
					
			# Replies not enough information
			else:
				self.push(IRCmessage(kwargs["destination"], "More info plzkthnxbia."), "4010", self.channel)
		
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "4010", self.channel)
			
	@listener("command-miniwallop")
	def miniwallop(self, **kwargs):
		"""Sends a wallop under the same conditions and in the same manor as the wallop
		function except the wallop is only sent to the current network."""
		# Checks for botadmin permissions
		if kwargs["botadmin"] >= 2:
			
			# Checks for proper arguments
			if len(kwargs["command"]) > 1:
		
				# Sends wallop
				self.cursor.execute("EXECUTE chan(%s);" % (self.channel.strip("Net"))
				data = self.cursor.fetchall()
				for row in data:
					self.push(IRCmessage(row[0], kwargs["command"][1:]), "4010", self.channel)
					
			# Replies not enough information
			else:
				self.push(IRCmessage(kwargs["destination"], "More info plzkthnxbia."), "4010", self.channel)
		
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "4010", self.channel)
			
	@listener("command-raw")
	def raw(self, **kwargs):
		"""If botadmin permissions allow and a command was given the command is
		sent to be executed as raw code. If conditions are not met a denied
		message is given."""
		# Checks for botadmin permissions
		if kwargs["botadmin"] == 3:
			
			# Checks for proper arguments
			if len(kwargs["command"]) > 1:
			
				# Issues override event
				self.push(RAW(kwargs["command"][1:]), "override", self.channel)
					
			# Replies not enough information
			else:
				self.push(IRCmessage(kwargs["destination"], "More info plzkthnxbia."), "5012", self.channel)
		
		# Replies not bot admin
		else:
			self.push(IRCmessage(kwargs["destination"], "You are not a bot administrator."), "4010", self.channel)
